package com.haier.tv.mic.detect;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.haier.tv.mic.detect.manager.HaierMicManager;
import com.haier.tv.mic.detect.util.LogUtil;

import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();
    private Button recordButton;
    private Button micDetectButton;
    private Button airMicDetectButton;
    private Button playButton;
    public MediaPlayer mMediaPlayer ;

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 0x00:
                    playButton.setEnabled(isFileExist("/storage/emulated/0/recorder/haier.wav"));
                    break;
                default:
                    break;
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMediaPlayer = new MediaPlayer();
        initView();
    }

    private void initView() {
        recordButton = findViewById(R.id.recorder_button);
        recordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (recordButton.getText().equals(getString(R.string.str_start_record))) {
                    recordButton.setText(getString(R.string.str_stop_record));
                    LogUtil.d(TAG,"start recording");
                    HrTvApi.getInstance().beforeMicRecordInit();
                    HaierMicManager.getHaierMicManager().micInit(6,16000);
                }else{
                    recordButton.setText(getString(R.string.str_start_record));
                    LogUtil.d(TAG,"stop recording");
                    HaierMicManager.getHaierMicManager().micDeInit();

                }
            }
        });

        micDetectButton = findViewById(R.id.mic_detect_button);
        micDetectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent("android.intent.action.start.mic.detect");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        airMicDetectButton = findViewById(R.id.air_detect_button);
        airMicDetectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent("android.intent.action.start.mic.air.detect");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        playButton = findViewById(R.id.play_button);
        playButton.setEnabled(isFileExist("/storage/emulated/0/recorder/haier.wav"));
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (playButton.getText().equals(getString(R.string.str_start_play))){
                    try {
                        mMediaPlayer.setDataSource("/storage/emulated/0/recorder/haier.wav");
                    } catch (IOException em) {
                        throw new RuntimeException(em);
                    }
                    try {
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                    } catch (IOException em) {
                        throw new RuntimeException(em);
                    }
                    playButton.setText(getString(R.string.str_stop_play));
                }else{

                    if (mMediaPlayer.isPlaying())
                        mMediaPlayer.stop();
                    mMediaPlayer.reset();
                    playButton.setText(getString(R.string.str_start_play));
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private boolean isFileExist(String path){

        File file = new File(path);
        return file.exists();
    }
}